vimfandomcom-20200223-history
Enhanced command window
If you are comfortable * with Vim's modes and * with using the key (meaning you rarely hit unnecessarily) and you * would prefer entering : commands in a modal command window rather than on the command line Then this tip is for you. Some key features of the command window (see ) that would lead one to be interested in it are: * One can edit the buffer any way one wants * Hitting results in the line one was on being executed * editing in the command window is much nicer than editing on the command line Basic use of the command window is easy, but this tip allows you to improve the command window with simple mappings to enter and leave it, as well as providing missing functionality from the command line such as using to view matches for what you have typed so far. Begin with some mappings to easily enter and leave the command window with a single key-stroke: nmap q:_ nmap q/ q/_ nmap q? q?_ augroup ECW_au au! au CmdwinEnter * nmap :q au CmdwinLeave * nmap q:_ augroup END Some nice things about the command line that are not present in the usual command window are: * The and arrow find all the commands that match the text entered to the left of the cursor * Hitting shows the ways in which the typed text can be completed (see ) It is possible to have similar features in the command-window too. For the feature, add the following au-command event triggered maps: augroup ECW_au " musn't do au! again au CmdwinEnter : imap y0:let@/='^'.@0? au CmdwinLeave : iunmap au CmdwinEnter : imap y0:let@/='^'.@0/ au CmdwinLeave : iunmap au CmdwinLeave : :let @/="" augroup END Now, while in the command window, going to insert mode and hitting the arrow followed by the n key results in one visiting all the commands that match the text to the left of the cursor when the key was hit – then hitting while on any line causes it to be executed. Likewise, for the arrow. Next for the feature. This is slightly more complex. Begin by adding the following autocommand event triggered maps: augroup ECW_au " musn't do au! again au CmdwinEnter : imap y0:ECWCtrlD au CmdwinLeave : iunmap augroup END Then provide this function: function! s:ECWCtrlD() With this function, hitting while in insert mode in the command window results in more information about the text to the left of the cursor appearing on the lines below the cursor. This information can left on the command window or removed by typing u (undo). The nature of the information provided by depends on what is to the left of the cursor: *If the stuff to the left of the cursor looks, essentially like "map " or "map foo" then the information provided by is the same as the information that appears when the the same stuff is typed on the command line and return is hit. *If the stuff to the left of the cursor begins, essentially like "sf " or like "find " then what is displayed on is the glob of the remaining stuff (after appending the remaining stuff with a *) *If the stuff to the left of the cursor looks like neither of the above two cases then what is displayed is the glob of the very last non-space separated "word" (after appending that "word" with a *) function! s:ECWCtrlD() if (match(@", '^ *a-z\?map\s\s*\(\S\S*\)\?\s*$') >=0 ) let s:foo = @" let save_more=&more set nomore execute ':redir @" |'.s:foo.'|redir END' let &more = save_more put=@" "Keep this next command even though Vim complains -- it is "a work-around for some "unknown bad thing" silent normal return endif "sf and find can have space separated arguments if (match(@", '^ *\(\(sf\)\|\(find\)\)\ *') >=0 ) let s:foo = substitute(@", '^ *\(\(sf\)\|\(find\)\)\ *', '', '') else "pick the trailing non-space separated stuff let s:foo = substitute(@", '\(.\{-}\)\(\S\S*\s*\)$', '\2', '') endif let s:foo = substitute(s:foo, '\s*$', '*', '') "OK if ending has two wild-cards let @"=glob(s:foo) if(@" "") | let @"='no match' | endif put=@" endfunction if !exists(":ECWCtrlD") command -nargs=0 ECWCtrlD call s:ECWCtrlD() endif If one wants to get even more fancy, one can start with the map nmap :ECWtobedefined wherein the function ECWtobedefined opens up a new buffer in which one can do anything one likes. Even if this new buffer merely mimics the command window, it will the feature of co-existing with other buffers -- which is a feature that the command window does not have! Comments *Define the undefined :ECWtobedefined, or remove it *Test whether this tip even works *: How does it work? I thought the first example needs s/nmap/nnoremap/, but it works. *Reword as desired for better "flow" and layout Still needs review. Only did minor rewordings. --Fritzophrenic 16:51, 28 July 2008 (UTC) ----